﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.Reports;

namespace ERP.QuanlyKho
{
    public partial class viewERP_TracuuTonkho : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_chitiet_nhapkhoInfo ct = new cs_erp_chitiet_nhapkhoInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        int count = 0;

        public viewERP_TracuuTonkho()
        {
            InitializeComponent();
        }

        private void viewERP_TracuuTonkho_Load(object sender, EventArgs e)
        {
            this.Load_Combo();
            this.CheckUser();
        }
        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                
                if (userright.printreport == 0)
                {
                    this.btnIn.Enabled = false;
                    this.btnIntg.Enabled = false;
                }
            }
        }
        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            //this.hanghoa.Get_List = this.kh.id_kh;
            try
            {
                this.ct.makho = Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim());
                ds = this.ct.Get_List_Tonkho();
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    this.count = 0;
                }
                else
                {
                    this.count = ds.Tables[0].Rows.Count;
                    this.grcDanhsach.DataSource = ds.Tables[0];
                    this.gridView1.ExpandAllGroups();
                }
            }
            catch
            {
                XtraMessageBox.Show("Bạn phải chọn kho hàng!", "Thông báo!");
            }
        }

        private void Load_Combo()
        {
            DataSet ds = new DataSet("ds");
            cs_erp_khohangInfo kho = new cs_erp_khohangInfo();
            ds = kho.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                //this.count = 0;
            }
            else
            {
                this.cboKhoxuat.Properties.DataSource = ds.Tables[0];
            }
        }

        private void cboKhoxuat_EditValueChanged(object sender, EventArgs e)
        {
            this.Load_Data();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            this.inHoadon();
        }

        private void inHoadon()
        {
            DataSet ds = new DataSet("ds");
            try
            {
                this.ct.makho = Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim());
                ds = this.ct.Get_List_Tonkho();
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    XtraMessageBox.Show("Không có số liệu!", "Thông báo!");
                    return;
                }
                
            }
            catch
            {
                XtraMessageBox.Show("Bạn phải chọn kho hàng!", "Thông báo!");
                return;
            }
            //Tinh so luong va tong tien
            double sl = 0;
            double tongtien = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                sl += Convert.ToDouble(ds.Tables[0].Rows[i]["soluong"].ToString().Trim());
                tongtien += Convert.ToDouble(ds.Tables[0].Rows[i]["tien"].ToString().Trim());
            }
            string ban1 = "Tính đến hết ngày: " + DateTime.Now.ToShortDateString();
            
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            dv.captren = "0";
            dv.Get_Info_by_Parents();
            rptTonkho rpt = new rptTonkho();
            rpt.DataSource = ds.Tables[0];
            rpt.ds_BaoCao = ds.Tables[0];
            //rpt.dsHoadon = this.hoadon.Get_HD_Tinhtien().Tables[0];
            rpt.ban = ban1;
            rpt.donvi = dv.tendonvi;
            rpt.diachi = dv.diachi;
            rpt.lblDienthoai.Text = dv.sodt_fax;
            rpt.lbNoidung.Text = this.cboKhoxuat.Text;
            rpt.lblSL.Text = sl.ToString();
            rpt.lblThanhtien.Text = tongtien.ToString();
            //rpt.lbGhichu.Text = this.nk.ghichu;
            rpt.In();
            rpt.ShowPreview();

        }

        private void btnIntg_Click(object sender, EventArgs e)
        {
            this.inHoadontg();
        }

        private void inHoadontg()
        {
            
            DataSet ds = new DataSet("ds");
            try
            {
                int ngay = 0;
                ngay = Convert.ToDateTime(this.txtNgay.EditValue).Year * 12 * 30 + Convert.ToDateTime(this.txtNgay.EditValue).Month * 30 + Convert.ToDateTime(this.txtNgay.EditValue).Day;

                this.ct.makho = Convert.ToInt32(this.cboKhoxuat.EditValue.ToString().Trim());
                ds = this.ct.Get_List_Tonkho_Truocngay(ngay);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    XtraMessageBox.Show("Không có số liệu!", "Thông báo!");
                    return;
                }
                
            }
            catch
            {
                XtraMessageBox.Show("Bạn phải chọn kho hàng!", "Thông báo!");
                return;
            }

            double sl = 0;
            double tongtien = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                sl += Convert.ToDouble(ds.Tables[0].Rows[i]["soluong"].ToString().Trim());
                tongtien += Convert.ToDouble(ds.Tables[0].Rows[i]["tien"].ToString().Trim());
            }
            string ban1 = "Tính đến hết ngày: " + Convert.ToDateTime(this.txtNgay.EditValue).ToShortDateString();

            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            dv.captren = "0";
            dv.Get_Info_by_Parents();
            rptTonkho rpt = new rptTonkho();
            rpt.DataSource = ds.Tables[0];
            rpt.ds_BaoCao = ds.Tables[0];
            //rpt.dsHoadon = this.hoadon.Get_HD_Tinhtien().Tables[0];
            rpt.ban = ban1;
            rpt.donvi = dv.tendonvi;
            rpt.diachi = dv.diachi;
            rpt.lblDienthoai.Text = dv.sodt_fax; 
            rpt.lbNoidung.Text = this.cboKhoxuat.Text;
            rpt.lblSL.Text = sl.ToString();
            rpt.lblThanhtien.Text = tongtien.ToString();
            //rpt.lbGhichu.Text = this.nk.ghichu;
            rpt.In();
            rpt.ShowPreview();

        }
    }
}